package com.sq.universe.system.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.FileEntity;
import com.sq.universe.system.entity.TableConfigurationEntity;
import com.sq.universe.system.service.AutoCodeService;
import com.sq.universe.system.service.TableConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
@RequestMapping("/autoCode")
@CrossOrigin
@Api(description = "代碼生產")
public class AutoCodeController extends BaseController {

    @Autowired
    private AutoCodeService autoCodeService;

    @Autowired
    private TableConfigurationService tableConfigurationService;

    @GetMapping("/index")
    @RequiresPermissions("system:autoCode:index")
    @ApiOperation("主頁")
    public String index(){
        return "/system/autoCode/index";
    }

    /**
     * 保存配置
     * @param tableConfigurationEntity
     * @return
     */
    @PostMapping("/saveConfiguration")
    @ResponseBody
    @RequiresPermissions("system:autoCode:saveConfiguration")
    public RespEntity saveConfiguratoin(@RequestBody(required = false) TableConfigurationEntity tableConfigurationEntity){
        tableConfigurationService.insert(tableConfigurationEntity);
        return new RespEntity("配置保存成功");
    }

    /**
     * 查询所有的表
     * @return
     */
    @PostMapping("/getTables")
    @ResponseBody
    @RequiresPermissions("system:autoCode:getTables")
    public RespEntity getTables(){
        return new RespEntity("查询成功", autoCodeService.getTables());
    }

    /**
     * 查询选中表的详情
     * @return
     */
    @PostMapping("/queryTableDetail")
    @ResponseBody
    @RequiresPermissions("system:autoCode:queryTableDetail")
    public RespEntity queryTableDetail(String tableName){
        return new RespEntity("查询成功", autoCodeService.queryTableDetail(tableName));
    }

    /**
     * 单表生成
     * @return
     */
    @PostMapping("/codeGenerateBySingle")
    @ResponseBody
    @RequiresPermissions("system:autoCode:codeGenerateBySingle")
    public RespEntity codeGenerateBySingle(@RequestBody(required = false) TableConfigurationEntity tableConfigurationEntity){
//        tableConfigurationService.insert(tableConfigurationEntity);
        autoCodeService.codeGenerateBySingle(tableConfigurationEntity);
        return new RespEntity("查询成功");

    }


    @RequestMapping("/download")
    public void download(@RequestBody(required = false) TableConfigurationEntity tableConfigurationEntity, HttpServletResponse response) {
        try {
            String filePath = autoCodeService.codeGenerateBySingle(tableConfigurationEntity);
            ZipUtil.zip(filePath);
            File tempFile = new File(filePath + ".zip");
            // 获取文件名
            String filename = tableConfigurationEntity.getTcTableName();
            // 获取文件后缀名
            String ext = "zip";
            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + tempFile.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(FileUtil.readBytes(tempFile));
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
